package com.huizhan.web.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.view.*;
import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

@Api(tags = "Source",description = "来源")
@RestController
public class SourceController {
    
    @Autowired
    private SourceDao dao;
   
    @Autowired
    private RegisterDao jdao;
   
    /**
     * 根据ID获取来源信息
     * @param id 来源ID
     * @return api结果
     */
    @ApiOperation(value = "根据项目ID获取来源信息")
    @RequestMapping(value =  "/api/Source/Project/{id}",method = {RequestMethod.GET})
    public ApiResult<List<Source>> Get(@PathVariable String id){
        ApiResult<List<Source>> result=new ApiResult<List<Source>>();
        try
        {
            result.setData(dao.findByProjectId(id));
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 查询赠票集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询来源集合")
    @RequestMapping(value = "/api/Source/Search",method = {RequestMethod.POST})
    public ApiResult<List<Source>> Search(@RequestBody PageProject model){
         ApiResult<List<Source>> result=new ApiResult<List<Source>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"source_Id") ;            
            Page<Source> page = dao.findByWhere(model.getName(),model.getProjectId(),model.getType(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        
        return result;
    }

    /**
     * 根据ID获取来源信息
     * @param id 来源ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取来源信息")
    @RequestMapping(value =  "/api/Source/{id}",method = {RequestMethod.GET})
    public ApiResult<Source> Get(@PathVariable Long id){
        ApiResult<Source> result=new ApiResult<Source>();
        try
        {
            Source model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改来源信息
     * @param id 来源ID
     * @param model 来源信息
     * @return api结果
     */
    @ApiOperation(value = "修改来源信息")
    @RequestMapping(value =  "/api/Source/{id}",method = {RequestMethod.PUT})
    public ApiResult<Source> Edit(@PathVariable Long id,@RequestBody Source model){
        ApiResult<Source> result=new ApiResult<Source>();
        try
        {
            Source source= dao.getOne(id);
            Register register=jdao.getOne(model.getRegisterId());
            
            if(register==null){
                result.setHasError(true);
                result.setMessage("注册通道不能为空");
            }else if(source==null){
                result.setHasError(true);
                result.setMessage("参数错误");
            }else if(dao.countByNameAndRegisterIdAndSourceIdNot(model.getName(), model.getRegisterId(), id)>0){
                result.setHasError(true);
                result.setMessage("名称已存在");
            }
            else{
                source.setType(register.getType());
                source.setChannel(register.getName());
                source.setUrl(register.getUrl()+"?sourceId="+id);
                source.setName(model.getName());
                dao.save(source);
                result.setData(model);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加来源信息
     * @param model 来源信息
     * @return api结果
     */
    @ApiOperation(value = "添加来源信息")
    @RequestMapping(value =  "/api/Source/",method = {RequestMethod.POST})
    public ApiResult<Source> Add(@RequestBody Source model){
         ApiResult<Source> result=new ApiResult<Source>();
        try
        {
            Register register=jdao.getOne(model.getRegisterId());

            if(register==null){
                result.setHasError(true);
                result.setMessage("注册通道不能为空");
            }else if(dao.findByNameAndRegisterId(model.getName(),model.getRegisterId())!=null){
                result.setHasError(true);
                result.setMessage("名称已存在");
            }else{
                model.setChannel(register.getName());
                model.setProjectId(register.getProjectId());
                model.setCreateDate(new Date());
                model.setType(register.getType());
                model.setHit(0L);
                model.setPerson(0L);
                dao.save(model);
                
                model.setUrl(register.getUrl()+"?sourceId="+model.getSourceId());

                dao.save(model);
                result.setData(model);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除来源信息
     * @param id 来源ID
     * @return api结果
     */
    @ApiOperation(value = "删除来源信息")
    @RequestMapping(value =  "/api/Source/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
           if(dao.existsById(id)){
                dao.deleteById(id);
           }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}